﻿#include <windows.h>
#include <comdef.h>
#include <atlbase.h>
#include <iostream>
#include <atlstr.h>

#import "dm.dll" no_namespace


#ifdef _DEBUG
#include "Debug\dm.tlh"
#else
#include "Release\dm.tlh"
#endif  // _DEBUG


// 显式加载dm.dll并注册COM组件
bool RegisterDmDll() {
    HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
    if (FAILED(hr)) {
        std::cerr << "COM初始化失败，错误码：" << std::hex << hr << std::endl;
        return false;
    }

    HINSTANCE hdllInst = LoadLibrary(L"dm.dll");
    if (!hdllInst) {
        std::cerr << "加载dm.dll失败" << std::endl;
        return false;
    }

    typedef HRESULT(__stdcall* pfnDllRegisterServer)();
    pfnDllRegisterServer pfnRegServer = (pfnDllRegisterServer)GetProcAddress(hdllInst, "DllRegisterServer");
    if (pfnRegServer) {
        hr = pfnRegServer();
        if (FAILED(hr)) {
            std::cerr << "注册dm.dll失败，错误码：" << std::hex << hr << std::endl;
            FreeLibrary(hdllInst);
            return false;
        }
    }
    else {
        std::cerr << "未找到DllRegisterServer函数" << std::endl;
        FreeLibrary(hdllInst);
        return false;
    }

    FreeLibrary(hdllInst);
    return true;
}

Idmsoft* GetDmObject() {
    Idmsoft* m_dm = NULL;
    HRESULT hr = CoCreateInstance(__uuidof(dmsoft), NULL, CLSCTX_INPROC_SERVER, __uuidof(Idmsoft), (void**)&m_dm);
    if (FAILED(hr)) {
        std::cerr << "创建COM对象失败，错误码：" << std::hex << hr << std::endl;
    }
    return m_dm;
}

int main(int argc, char* argv[]) 
{
    // 自动注册dm.dll
    if (!RegisterDmDll()) {
        return -1;
    }

    try {
        Idmsoft* dm = GetDmObject();
        if (!dm) {
            throw std::runtime_error("获取dmsoft对象失败");
        }

        // 注册软件（仅需一次）
        VARIANT_BOOL regResult = dm->Reg(L"juzhihai5d18cfebb46360dd000bc0fe9874a6fd", L"bG5xHd");
        if (regResult != true) {
            throw std::runtime_error("软件注册失败");
			MessageBox(NULL, L"软件注册失败", L"错误", MB_ICONERROR);
        }

        //std::wcout << L"版本号：" << dm->Ver().c_str() << std::endl;

        // 内存操作（示例）
        DWORD pid = 23028; // 目标进程ID
        HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
        if (!hProcess) {
            throw std::runtime_error("打开进程失败");
        }

        int handle = (int)GetModuleHandle(L"dm.dll");
        dm->DmGuard(1, L"memory2");
        dm->SetMemoryHwndAsProcessId(1);
        dm->SetAsmHwndAsProcessId(1);

        std::wcout << L"进程ID：" << pid << L"模块句柄：" << handle << std::endl;

        // 示例：获取模块基址
        LONGLONG moduleBase = dm->GetModuleBaseAddr(pid, "R2Client-Win64-Shipping.exe");
        //LONGLONG moduleBase = dm->GetModuleBaseAddr(pid, (_bstr_t)(0x7FF726A50000));
        std::wcout << L"模块基址：" << moduleBase << std::endl;

        // 示例：模拟按键
        //dm->MoveTo(100, 100);
        //dm->delay(3000);
        //dm->KeyPressStr(L"1234567890", 10);
        // 释放COM对象
        dm->Release();
    }
    catch (const _com_error& e) {
        std::cerr << "COM错误：" << e.ErrorMessage() << " (错误码：" << std::hex << e.Error() << ")" << std::endl;
    }
    catch (const std::exception& e) {
        std::cerr << "运行时错误：" << e.what() << std::endl;
    }

    ::CoUninitialize();
    return 0;
}